/**
 * Created by yangxianming on 2015-04-30.
 */

import models.MyConfig;
import tools.ReadProperties;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.Random;

// 扩展 HttpServlet 类
@WebServlet(name = "test1", urlPatterns = {"/test1"}, loadOnStartup = 2)
public class Test1 extends HttpServlet {
    MyConfig myConfig = new MyConfig();
    Properties properties;

    @Override
    public void init(ServletConfig config) throws ServletException {
        ReadProperties readProperties = new ReadProperties(config.getServletContext().getRealPath("/"));
        myConfig.setPropertiesConfig(readProperties);
        this.properties = readProperties.getProperties();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        long start = new Date().getTime();
        PrintWriter out = response.getWriter();

        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = sdf1.format(new Date());

        response.setHeader("Content-type", "text/html;charset=UTF-8");
        String fromIp = request.getRemoteAddr();
        String string = new String(request.getParameter("string").getBytes(),"UTF-8");
//        System.out.println("ip:" + fromIp);
//        System.out.println("string:" + string);

        String path = myConfig.getPropertiesConfig().getProperties().getProperty("appPath");
        File file = new File(path + "//data//record.txt");
        FileOutputStream fileOutputStream = null;
        OutputStreamWriter outputStreamWriter = null;
        try {
            fileOutputStream = new FileOutputStream(file, true);
            outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8");
            outputStreamWriter.write(time + "; " + fromIp + "; " + string + "\n");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (outputStreamWriter != null){
                try {
                    outputStreamWriter.flush();
                    outputStreamWriter.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }

        try {
            Random random = new Random();
            int s = random.nextInt(61);
//            Thread.sleep(s * 1000);
        }catch (Exception e){
            e.printStackTrace();
        }

        out.print(string);

        //end
        long end = new Date().getTime();
        System.out.println(request.getRequestURI() + "; run time: " + (end-start) + "ms");

    }

    public void destroy()
    {
        // 什么也不做
    }

}